package ${packagePrefix}.application.executor.${tableName?lower_case};

import ${packagePrefix}.application.assembler.${domainObjectName}Assembler;
import ${packagePrefix}.client.dto.command.${domainObjectName}ModifyCmd;
import ${packagePrefix}.domain.gateway.${domainObjectName}Gateway;
import ${packagePrefix}.domain.model.${tableName?lower_case}.${domainObjectName};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.cola.dto.Response;
import org.springframework.transaction.annotation.Transactional;

/**
 * ${entityComment}修改命令执行器
 */
@Component
public class ${domainObjectName}ModifyCmdExe {

    @Autowired
    private ${domainObjectName}Gateway ${domainObjectName?uncap_first}Gateway;

    @Autowired
    private ${domainObjectName}Assembler ${domainObjectName?uncap_first}Assembler;

    /**
     * 执行${entityComment}修改命令
     *
     * @param cmd ${entityComment}修改命令
     * @return 执行结果
     */
    @Transactional
    public Response execute(${domainObjectName}ModifyCmd cmd) {
        // 查询领域实体
        ${domainObjectName} ${domainObjectName?uncap_first} = ${domainObjectName?uncap_first}Gateway.findById(cmd.getId())
                .orElseThrow(() -> new RuntimeException("${entityComment}不存在"));
        
        // 更新领域实体
        ${domainObjectName?uncap_first}Assembler.updateEntity(${domainObjectName?uncap_first}, cmd);
        
        // 进行业务规则校验
        ${domainObjectName?uncap_first}.checkValid();
        
        // 保存领域实体
        ${domainObjectName?uncap_first}Gateway.save(${domainObjectName?uncap_first});
        
        return Response.buildSuccess();
    }
} 